package com.ngrok.handler;

import com.ngrok.NgrokContext;
import com.ngrok.client.NgrokClient;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.util.CharsetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by pj on 2017/9/29.
 */
public class LocalHttpHandler extends SimpleChannelInboundHandler<ByteBuf> {
    private final static Logger loger= LoggerFactory.getLogger(LocalHttpHandler.class);
    private NgrokContext context;
    public LocalHttpHandler ( NgrokContext context){
        this.context=context;
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf httpObject) throws Exception {
        byte[] bytes=new byte[httpObject.readableBytes()];
        httpObject.readBytes(bytes);
        String das=new String(bytes);
        System.out.println(Thread.currentThread().getName());
        channelHandlerContext.disconnect().addListener(future -> {
           if(future.isSuccess()){
               System.out.println("本地链接关闭");

           }
        });
        context.getRemotechannel().writeAndFlush(Unpooled.copiedBuffer(das, CharsetUtil.UTF_8));
    }
}
